/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.web.actions.usuarios;


import com.ejemplo.bt.monedas.services.MonedaServices;
import com.ejemplo.bt.monedas.vo.Moneda;
import com.web.actions.AbstractAction;
import com.web.actions.usuarios.vo.MonedaEditorActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Usuario
 */
public class MonedaEditorAction extends AbstractAction {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ActionErrors errors = new ActionErrors();

        try {
        MonedaServices services = new MonedaServices();
        MonedaEditorActionForm datosValidados = new MonedaEditorActionForm();

        long idMoneda = 0;
        try {  
            idMoneda = new Long((String)request.getParameter("idMoneda")).longValue();
        }
        catch (NumberFormatException nfex) {
            try {
                idMoneda = ((Long) request.getAttribute("idMoneda")).longValue();
            }
            catch (Exception ex) {
                idMoneda = datosValidados.getIdMoneda();
            }
        }      

        datosValidados.setIdMoneda(idMoneda);
        // chequeo si estoy creando una moneda o si estoy editando una ya existente
        if (idMoneda == 0){
            datosValidados.setIdMoneda(0);
            datosValidados.setMoneda(null);
        }
        else {
            //recupero la moneda
            Moneda moneda = services.obtenerMoneda(idMoneda);

            datosValidados.setIdMoneda(moneda.getIdMoneda());
            datosValidados.setMoneda(moneda.getMoneda());
        }

        // cargo los datos validados en el request
        request.setAttribute("datosValidados", datosValidados);

        // Forwardeo
        return mapping.findForward("success");
        }
        catch (Exception ex) {
            ex.printStackTrace();
            // Seteo los errores en el request.
            errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.message",
                ex.getMessage()));
            saveErrors(request, errors);

            // Forwardeo.
            return mapping.findForward("failure");
        }
    }
}
